package com.info.zhiduoduo.weixin.service;

import com.google.common.collect.Lists;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.StringUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.system.AddressBriefDto;
import com.info.zhiduoduo.repository.mapper.system.AddressMapper;
import com.info.zhiduoduo.weixin.formbean.system.SearchAddressFormBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressService extends BaseService {

	@Autowired
	AddressMapper addressMapper;

	/**
	 匹配省份

	 @param formBean
	 @return
	 */
	public SmartResult getProvince(BaseSimpleFormBean<SearchAddressFormBean> formBean) {
		SearchAddressFormBean reqdata = formBean.getReqdata();
		String name = reqdata.getName();
		if (StringUtil.isEmpty(name)) name = "";
		return smartResult(addressMapper.getProvince(name));
	}

	public SmartResult getCity(BaseSimpleFormBean<SearchAddressFormBean> formBean) {
		SearchAddressFormBean reqdata = formBean.getReqdata();
		String name = reqdata.getName();
		String provinceCode = reqdata.getProvinceCode();
		if (StringUtil.isEmpty(name)) name = "";
		if (StringUtil.isEmpty(provinceCode)) provinceCode = "";
		return smartResult(addressMapper.getCity(provinceCode, name));
	}

	public SmartResult getCounty(BaseSimpleFormBean<SearchAddressFormBean> formBean) {
		SearchAddressFormBean reqdata = formBean.getReqdata();
		String name = reqdata.getName();
		String provinceCode = reqdata.getProvinceCode();
		String cityCode = reqdata.getCityCode();
		if (StringUtil.isEmpty(name)) name = "";
		if (StringUtil.isEmpty(provinceCode)) provinceCode = "";
		if (StringUtil.isEmpty(cityCode)) cityCode = "";
		return smartResult(addressMapper.getCounty(provinceCode, cityCode, name));
	}

	public SmartResult getAllAddr(BaseSimpleFormBean formBean) {
		List<AddressBriefDto> addrList = addressMapper.getProvince("").parallelStream().map(province -> {
			AddressBriefDto provinceDto = new AddressBriefDto();
			provinceDto.setId(province.getId());
			provinceDto.setName(province.getName());
			List<AddressBriefDto> citylist = addressMapper.getCity(province.getProvinceCode(), "").parallelStream().map(city -> {
				AddressBriefDto cityDto = new AddressBriefDto();
				cityDto.setId(city.getId());
				cityDto.setName(city.getName());

				List<AddressBriefDto> countyList = addressMapper.getCounty(city.getProvinceCode(), city.getCityCode(), "")
						.stream()
						.map(county -> {
							AddressBriefDto countyDto = new AddressBriefDto();
							countyDto.setId(county.getId());
							countyDto.setName(county.getName());
							countyDto.setChildren(Lists.newArrayList());
							return countyDto;
						})
						.collect(Collectors.toList());
				if (countyList == null) countyList = new ArrayList<>();
				cityDto.setChildren(countyList);
				return cityDto;
			}).collect(Collectors.toList());
			if (citylist == null) citylist = new ArrayList<>();
			provinceDto.setChildren(citylist);
			return provinceDto;
		}).collect(Collectors.toList());
		return smartResult(addrList);
	}

	/**
	 获取所有城市列表

	 @param formBean
	 @return
	 */
	public SmartResult getAllCity(BaseSimpleFormBean formBean) {
		List<AddressBriefDto> list = addressMapper.getAddrByLevel(2).stream().map(addr -> {
			AddressBriefDto briefDto = new AddressBriefDto();
			briefDto.setId(addr.getId());
			briefDto.setName(addr.getName());
			return briefDto;
		}).collect(Collectors.toList());
		return smartResult(list);
	}
}
